package com.zhu.transform.util;

import com.zhu.transform.myenum.LogType;

import java.io.*;
import java.lang.reflect.Field;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author yingzhi zhu
 * date 2017/11/16.
 */
public class ZhuUtil {
    public static int number = 0;
    public static boolean isFirstCreatSn = false;


    public static String creatSn(int sn) {
        return String.format("S%09d", sn);
    }

    public static int getLastSn() {
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        int sn = 0;
        try {
            inputStreamReader = new FileReader("number.txt");
            bufferedReader = new BufferedReader(inputStreamReader);
            String lastSn = bufferedReader.readLine();
            sn = Integer.parseInt(lastSn);

        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        } finally {
            try {
                inputStreamReader.close();
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
        ZhuUtil.printLog(String.format("sn为%d", sn), LogType.INFO);
        return sn;
    }

    public static void saveSnToFile(int sn) {
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter("number.txt"));
            bufferedWriter.write(String.valueOf(sn));

        } catch (IOException e) {
            e.printStackTrace();
            ZhuUtil.printLog("number写入失败", LogType.INFO);
            System.exit(0);
        } finally {
            try {
                bufferedWriter.flush();
                bufferedWriter.close();

            } catch (IOException e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
    }

    //获取商品的折扣价
    public static String getTtldiscount(String discount, String totalAmount, String rounding) {
        double dbdiscount = Double.valueOf(discount);
        double dbtotalAmount = Double.valueOf(totalAmount);
        double dbrounding = Double.valueOf(rounding);
        DecimalFormat df = new DecimalFormat("######0.00");
        return df.format((dbtotalAmount + dbrounding) / (dbdiscount / (double) 100) - dbtotalAmount);
    }

    public static String foldIsExist(String fold, String filename, String suffix) {
        File file = null;
        try {
            file = new File(fold);
            if (!file.exists()) {
                file.mkdirs();
            }
            file = new File(fold, filename + suffix);
            file.createNewFile();

        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(file.getPath());
        return file.getPath();
    }


    public static String getTimeByFormat(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date = new Date();

        return simpleDateFormat.format(date);

    }

    /**
     * 获取今天的开始时间
     *
     * @return
     */
    public static String getTodayStartTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        return simpleDateFormat.format(date) + " 00:00:00";
    }

//    /**
//     * 获取今天的任意指定时间
//     *
//     * @return
//     */
//    public static Date getTodayStartTime(String time) {
//        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
//        String today = dayFormat.format(new Date());
//
//        SimpleDateFormat afterFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
//
//        try {
//            return afterFormat.parse(today+" "+time);
//        } catch (ParseException e) {
//            e.printStackTrace();
//            System.out.println("时间格式配置错误 请检查配置文件 每天上传的时间");
//            System.exit(0);
//        }
//        return null;
//    }


    /**
     * 获取今天的任意指定时间
     *
     * @return
     */
    public static Date getTodayStartTime(String time) {
        SimpleDateFormat dayFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = dayFormat.format(new Date());

        SimpleDateFormat afterFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            Date targetDate = afterFormat.parse(today + " " + time);
            //如果设置的HHMM 时间大于现在的时间
            if (targetDate.getTime() < System.currentTimeMillis()) {
                //定时任务设置成明天这个时候
                return new Date(targetDate.getTime() + 24 * 60 * 60 * 1000L);
            } else {
                return targetDate;
            }
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("时间格式配置错误 请检查配置文件 每天上传的时间");
            System.exit(0);
        }
        return null;
    }

    /**
     * 获取今天的结束时间
     *
     * @return
     */
    public static String getNowTime() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        return simpleDateFormat.format(date);
    }

    public static String getFormatByDate(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }


    public static String getTimeByTimeAndFormat(String time, String beforeFromat, String afterFormat) {
        SimpleDateFormat simpleFormatter = new SimpleDateFormat(beforeFromat);
        Date date = null;
        SimpleDateFormat targetSimpleFormatter = null;
        Date targetDate = null;
        try {

            date = simpleFormatter.parse(time);


            targetSimpleFormatter = new SimpleDateFormat(afterFormat);
            targetDate = new Date(date.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            System.out.println("chuchuo");
        }
        return targetSimpleFormatter.format(targetDate);

    }

    /**
     * 通过对象参数名把值动态注入到对象中
     */
    public static void dynamicInjectValue(Object obj, String name, String value) {
        try {
            Field[] fields = obj.getClass().getDeclaredFields();
            for (Field field : fields) {
                if (name.equals(field.getName())) {
                    field.setAccessible(true);
                    field.set(obj, matchFiledType(field, value));
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 当参数类型不是String时参数强制转化成int或者Long类型
     *
     * @param field
     * @param str
     * @return
     */
    private static Object matchFiledType(Field field, String str) {
        Object value = null;
        if (field.getType().getName().contains("Long")) {
            value = Long.decode(str);
        } else if (field.getType().getName().contains("int")) {
            value = Integer.decode(str);
        } else if (field.getType().getName().contains("double")) {
            value = Double.valueOf(str);
        } else {
            value = str;
        }
        return value;
    }


    public static void printLog(String message, LogType logType) {
        System.out.println(String.format("%s : %s : %s", getNowTime(), logType.toString(), message));
    }

}
